/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package auctions;

import java.util.Arrays;
import java.util.ArrayList;

/**
 *
 * @author hangsu
 */
public class ScheduleAscendingAuction extends ScheduleAuction{
    
    private double lastBid;
    
    private int lastWinner;

    public ScheduleAscendingAuction(int auctionIdx) {
        super(auctionIdx, AuctionType.ASCENDING);
        lastBid=this.reservePrice-1;
    }
    
    @Override
    public void initializeBidders() {
        /* report reserve pice here */
        AuctionStatus status = new AuctionStatus(this.auctionIdx, -1, lastBid, reservePrice);
        for(ScheduleAgent a : this.agents)
            a.updateStatus(status);
    }
    
    @Override
    public void runAuction() {
        
        //this.clear();
        this.quiet = true;
        int agentIdx;
        
        Double[] bids = new Double[this.bidderNum];
        double maxBid=0;
        int maxBidder=-1;
        
        for (agentIdx=0; agentIdx<this.bidderNum; agentIdx++) {
            bids[agentIdx] = this.agents.get(agentIdx).getBid();
            if(bids[agentIdx]>=this.lastBid+1) {
                this.quiet=false;
                if(bids[agentIdx]>maxBid) {
                    maxBid=bids[agentIdx];
                    maxBidder=agentIdx;
                }
            }
        }
        
        ArrayList<Double> newBids = new ArrayList(Arrays.asList(bids));
        this.result.setBidsNewRound(newBids);
        this.lastBid=maxBid;
        this.lastWinner=maxBidder;
        
        AuctionStatus status = new AuctionStatus(this.auctionIdx, lastWinner, lastBid, lastBid);
        
        /* report current info to bidders */
        for (ScheduleAgent a : this.agents)
            a.updateStatus(status);
        
        /* tmp result */
        this.result.setFinalResult(status);
        
        return ;
        
    }
    
}
